package org.jeecg.modules.columnmanager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.constant.RedisConstant;
import org.jeecg.modules.columnmanager.mapper.ColumnManagerMapper;
import org.jeecg.modules.columnmanager.service.IColumnManagerService;
import org.jeecg.modules.columnmanager.vo.ColumnManagerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 评论
 * @Author: jeecg-boot
 * @Date:   2020-11-12
 * @Version: V1.0
 */
@Service
public class ColumnManagerServiceImpl extends ServiceImpl<ColumnManagerMapper, ColumnManagerVO> implements IColumnManagerService {
    @Autowired
    private ColumnManagerMapper columnManagerMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public IPage<ColumnManagerVO> myPage(Page<ColumnManagerVO> page) {
        Object o = redisUtil.get(RedisConstant.ALL_COLUMN_INFO);
        List<ColumnManagerVO> columnInfo = null;
        if (!Objects.isNull(o)){
            // redis缓存中存在
            columnInfo = (List<ColumnManagerVO>) o;
        }else {
            System.out.println("更新缓存");
            columnInfo = columnManagerMapper.myPage(page);
            columnInfo.forEach(info->{
                info.setColumnImageString(info.getcolumnImageString());
            });
//            redisUtil.set(RedisConstant.ALL_COLUMN_INFO,columnInfo, TimeUnit.DAYS.toSeconds(1));
        }
        return page.setRecords(columnInfo);
    }

    @Override
    public void myDelete(String id) {
        columnManagerMapper.myDelete(id);
//        redisUtil.del(RedisConstant.ALL_COLUMN_INFO);
    }

    @Override
    public String getIdByName(String name) {

        return columnManagerMapper.getIdByName(name);
    }


    @Override
    public List<ColumnManagerVO> queryAllColumn() {
        Object o = redisUtil.get(RedisConstant.ALL_COLUMN_INFO);
        List<ColumnManagerVO> columnInfo = null;
        if (!Objects.isNull(o)){
            // redis缓存中存在
            columnInfo = (List<ColumnManagerVO>) o;
        }else {
            columnInfo = columnManagerMapper.myPage(new Page<ColumnManagerVO>());
        }
        return columnInfo;
    }

    @Override
    public List<ColumnManagerVO> queryNotPage() {
        return columnManagerMapper.queryNotPage();
    }

}
